/**
 * File: FilterArcsStdDist.java
 * Created by: mhaimel
 * Created on: Sep 29, 2011
 * CVS:  $Id: FilterArcsStdDist.java 1.0 Sep 29, 2011 11:03:37 AM mhaimel Exp $
 */
package uk.ac.ebi.curtain.controller.graph;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import uk.ac.ebi.curtain.controller.SingleSubmitController;
import uk.ac.ebi.curtain.model.graph.Graph;
import uk.ac.ebi.curtain.model.graph.curtain.ContigInfo;
import uk.ac.ebi.curtain.model.graph.curtain.CurtainGraph;
import uk.ac.ebi.curtain.model.graph.curtain.ReadWrapper;
import uk.ac.ebi.curtain.model.graph.curtain.filter.ArcMultiplicityMaximimCutoff;

/**
 * @author mhaimel
 *
 */
public class FilterArcsMultiplicityMax extends SingleSubmitController<Serializable, Serializable> {

	@Override
	protected void singleSubmit(List<Serializable> inputList) {
		CurtainGraph curtainGraph = getContext().getGraph();
		Graph<ContigInfo, ReadWrapper> graph = curtainGraph.getGraph();
		Integer categories = curtainGraph.getCategories();
		// reindex before starting
		graph.reindexArcs();
		graph.buildArcsIndex();
		
		getLog().info("Filter Arcs based on Maximum Multiplicity of Graph ... ");
		filterArcs(graph, categories);
		
		for(Serializable s : inputList){
			submit(s);
		}	
	}

	private void filterArcs(Graph<ContigInfo, ReadWrapper> graph,Integer categories) {
		int[] maxCutoff = new int[categories];

		for (int i = 0; i < categories; ++i){
			Integer coff = getContext().getArguments().getMaxPairCount(i);
			if(null == coff){
				coff = -1;
			}
			maxCutoff[i] =  coff;
		}
		getLog().info("Multiplicity cutoffs: max("  + Arrays.toString(maxCutoff)+");");
		graph.processArcs(new ArcMultiplicityMaximimCutoff(maxCutoff));
		
		// reindex 
		graph.buildArcsIndex();
		graph.reindexArcs();		
	}

}
